package com.lsx.controller;

import com.lsx.pojo.Category;
import com.lsx.pojo.Detailed;
import com.lsx.pojo.User;
import com.lsx.service.impl.CategoryServiceImpl;
import com.lsx.service.impl.DetailedServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by pc on 2017/11/6.
 */
@Controller
@RequestMapping("detal")
public class DetailedController {

    @Autowired
    private DetailedServiceImpl dtalService;
    @Autowired
    private CategoryServiceImpl ctyService;


    @RequestMapping(value = "getAllCategory")
    @ResponseBody
    public List<Category> getAllCategory(HttpSession session){
        User user=(User)session.getAttribute("user");
        List<Category> cty=ctyService.getById(user.getId());
        if(cty.size()==0){
            Category catgy=new Category();
            catgy.setCname("默认类别");
            catgy.setUserId(user.getId());
            int cos=ctyService.addCty(catgy);
            if(cos==1){
                cty.add(catgy);
            }
        }
        return cty;
    }

    @RequestMapping(value = "getAllDetal")
    @ResponseBody
    public List<Detailed> getAllDetal(HttpSession session,int cid){
        User user=(User)session.getAttribute("user");
        List<Detailed> dtal=dtalService.getall(user.getId(),cid);
        return dtal;
    }

    @RequestMapping(value = "getBySta",method = RequestMethod.GET)
    @ResponseBody
    public List<Detailed> getBySta(int sta){
        List<Detailed> dtal=dtalService.getBySta(sta);
        return dtal;
    }

    @RequestMapping(value = "addDatal")
    @ResponseBody
    public Detailed addDatal(String dname, String content,int cid,HttpSession session){
        User user=(User)session.getAttribute("user");
        Detailed dta=new Detailed();
        dta.setDname(dname);
        dta.setContent(content);
        SimpleDateFormat cd = new SimpleDateFormat("yyyy-MM-dd");
        String createdate=cd.format(new Date());
        SimpleDateFormat ct = new SimpleDateFormat("HH:mm:ss");
        String createtime=ct.format(new Date());
        dta.setCreatetime(createtime);
        dta.setCreatedate(createdate);
        dta.setUserId(user.getId());
        dta.setCid(cid);
        dtalService.addAll(dta);
        Detailed detal=dtalService.getById(dta.getId());
        return detal;
    }

    @RequestMapping(value = "updateDetal")
    @ResponseBody
    public Detailed updateDetal(String dname,String content,int state,int id){
        Detailed dta=new Detailed();
        dta.setDname(dname);
        dta.setContent(content);
        dta.setState(state);
        dta.setId(id);
        int cos=dtalService.updateStaue(dta);
        if(cos==1){
            Detailed detal=dtalService.getById(id);
            return detal;
        }else{
            return null;
        }
    }

    @RequestMapping(value = "detalDetal")
    @ResponseBody
    public int detalDetal(int id){
        int cos=dtalService.delete(id);
        return cos;
    }

}
